﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class BaojiaService
    {
        #region 新增
        /// <summary>
        /// 新增一条报价记录
        /// </summary>
        /// <param name="baojia"></param>
        /// <returns></returns>
        public int InsertBaojia(Baojia baojia)
        {
            string sql = "insert into Baojia(WorkParentId,Number,Customer,";
            sql += "Class,CostType,Requirement,Sales,Success)";
            sql += "values(@WorkParentId,@Number,@Customer,@Class,@CostType,";
            sql += "@Requirement,@Sales,@Success)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",baojia.WorkParentId),
                new SqlParameter("@Number",baojia.Number),
                new SqlParameter("@Customer",baojia.Customer),
                new SqlParameter("@Class",baojia.Class),
                new SqlParameter("@CostType",baojia.CostType),
                new SqlParameter("@Requirement",baojia.Requirement),
                new SqlParameter("@Sales",baojia.Sales),
                new SqlParameter("@Success",baojia.Success),
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据WorkParentId查询报价表中是否有记录
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public int GetBaojia(int workParentId)
        {
            string sql = "select count(*) from Baojia where WorkParentId=@WorkParentId";
            return Convert.ToInt32(SQLHelper.GetSingleResult(sql, new SqlParameter[] { new SqlParameter("@WorkParentId", workParentId) }));
        }
        /// <summary>
        /// 获取报价信息
        /// 需要参数：WorkParentId
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public Baojia GetBaojiaInfo(int workParentId)
        {
            string sql = "select * from BaoJia where WorkParentId=@WorkParentId";
            SqlDataReader objReader = SQLHelper.GetReader(sql, new SqlParameter[] { new SqlParameter("@WorkParentId", workParentId) });
            Baojia objBaojia = null;
            if(objReader.Read())
            {
                objBaojia = new Baojia()
                {
                    Id = Convert.ToInt32(objReader["Id"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    Number = objReader["Number"].ToString(),
                    Customer = objReader["Customer"].ToString(),
                    Class = objReader["Class"].ToString(),
                    CostType = objReader["CostType"].ToString(),
                    Requirement = Convert.ToDouble(objReader["Requirement"]),
                    Sales = Convert.ToDouble(objReader["Sales"]),
                    Success = Convert.ToChar(objReader["Success"])
                };

                
            }
            return objBaojia;
        }
        /// <summary>
        /// 获取最大Id行的Number值
        /// </summary>
        /// <returns></returns>
        public string GetLastNumber()
        {
            string sql = "select Number from BaoJia where Id=(select Max(Id) from BaoJia)";
            return Convert.ToString(SQLHelper.GetSingleResult(sql));
        }
        #endregion

        #region 更新
        public int UpdateBaojia(Baojia baojia)
        {
            string sql = "update BaoJia set Number=@Number,Customer=@Customer,Class=@Class,";
            sql += "CostType=@CostType,Requirement=@Requirement,Sales=@Sales,Success=@Success";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@Number",baojia.Number),
                new SqlParameter("@Customer",baojia.Customer),
                new SqlParameter("@Class",baojia.Class),
                new SqlParameter("@CostType",baojia.CostType),
                new SqlParameter("@Requirement",baojia.Requirement),
                new SqlParameter("@Sales",baojia.Sales),
                new SqlParameter("@Success",baojia.Success),
                new SqlParameter("@WorkParentId",baojia.WorkParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion
    }
}
